package model

import (
	"github.com/gomodule/redigo/redis"
	"strconv"
	"time"
	"tizhi_front/nspel"
)

type SelfEvaluation struct {
	Id                         int64     `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	StudentId                  int64     `gorm:"column:student_id"`
	Title                      string    `gorm:"column:title"`
	Height                     float64   `gorm:"column:height"`
	Weight                     float64   `gorm:"column:weight"`
	BodyMassIndex              float64   `gorm:"column:body_mass_index"`
	BodyMassIndexScore         float64   `gorm:"column:body_mass_index_score"`
	VitalCapacity              float64   `gorm:"column:vital_capacity"`
	VitalCapacityScore         float64   `gorm:"column:vital_capacity_score"`
	FiftyMeters                float64   `gorm:"column:50_meters"`
	FiftyMetersScore           float64   `gorm:"column:50_meters_score"`
	SitAndReach                float64   `gorm:"column:sit_and_reach"`
	SitAndReachScore           float64   `gorm:"column:sit_and_reach_score"`
	OneMinuteRopeSkipping      float64   `gorm:"column:1_minute_rope_skipping"`
	OneMinuteRopeSkippingScore float64   `gorm:"column:1_minute_rope_skipping_score"`
	OneMinuteSitUp             float64   `gorm:"column:1_minute_sit_up"`
	OneMinuteSipUpScore        float64   `gorm:"column:1_minute_sit_up_score"`
	CreatedAt                  time.Time `gorm:"column:created_at"`
	UpdatedAt                  time.Time `gorm:"column:updated_at"`
}

func (*SelfEvaluation) TableName() string {
	return "self_evaluation"
}

type SelfEvaluation0 struct {
	StudentId                  int64   `gorm:"column:student_id"`
	Title                      string  `gorm:"column:title"`
	Height                     float64 `gorm:"column:height"`
	Weight                     float64 `gorm:"column:weight"`
	BodyMassIndex              float64 `gorm:"column:body_mass_index"`
	BodyMassIndexScore         float64 `gorm:"column:body_mass_index_score"`
	VitalCapacity              float64 `gorm:"column:vital_capacity"`
	VitalCapacityScore         float64 `gorm:"column:vital_capacity_score"`
	FiftyMeters                float64 `gorm:"column:50_meters"`
	FiftyMetersScore           float64 `gorm:"column:50_meters_score"`
	SitAndReach                float64 `gorm:"column:sit_and_reach"`
	SitAndReachScore           float64 `gorm:"column:sit_and_reach_score"`
	OneMinuteRopeSkipping      float64 `gorm:"column:1_minute_rope_skipping"`
	OneMinuteRopeSkippingScore float64 `gorm:"column:1_minute_rope_skipping_score"`
	OneMinuteSitUp             float64 `gorm:"column:1_minute_sit_up"`
	OneMinuteSipUpScore        float64 `gorm:"column:1_minute_sit_up_score"`
}

func (*SelfEvaluation0) TableName() string {
	return "self_evaluation"
}

func (s *SelfEvaluation0) Insert() (err error) {
	err = nspel.GetOrm().Create(s).Error
	if err != nil {
		return
	}
	_ = nspel.RedisKeysDelete("self_evaluation1.student_id:" + strconv.FormatInt(s.StudentId, 10) + ".created_at.desc.all")
	return
}

func (s *SelfEvaluation0) UpdateByIdStudentId(id int64) (err error) {
	err = nspel.GetOrm().Table(s.TableName()).Where("id = ?", id).Where("student_id = ?", s.StudentId).Update(s).Error
	if err != nil {
		return
	}
	_ = nspel.RedisKeysDelete("self_evaluation1.student_id:" + strconv.FormatInt(s.StudentId, 10) + ".created_at.desc.all")
	return
}

type SelfEvaluationE struct{}

func (*SelfEvaluationE) TableName() string {
	return "self_evaluation"
}

func (*SelfEvaluationE) SelectAllByStudentIdOrderByCreatedAt(studentId int64) (selfEvaluations []SelfEvaluation1, err error) {
	key := "self_evaluation1.student_id:" + strconv.FormatInt(studentId, 10) + ".created_at.desc.all"
	err = nspel.RedisGetGob(key, &selfEvaluations)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"id", "title", "height", "weight", "body_mass_index", "body_mass_index_score", "vital_capacity", "vital_capacity_score", "50_meters", "50_meters_score", "sit_and_reach", "sit_and_reach_score", "1_minute_rope_skipping", "1_minute_rope_skipping_score", "1_minute_sit_up", "1_minute_sit_up_score", "created_at"}).Where("student_id = ?", studentId).Order("created_at desc").Find(&selfEvaluations).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, selfEvaluations)
	return
}

func (s *SelfEvaluationE) DeleteByIdStudentId(id int64, studentId int64) (err error) {
	err = nspel.GetOrm().Where("id = ?", id).Where("student_id = ?", studentId).Delete(s).Error
	if err != nil {
		return
	}
	_ = nspel.RedisKeysDelete("self_evaluation1.student_id:" + strconv.FormatInt(studentId, 10) + ".created_at.desc.all")
	return
}

type SelfEvaluation1 struct {
	Id                         int64     `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	Title                      string    `gorm:"column:title"`
	Height                     float64   `gorm:"column:height"`
	Weight                     float64   `gorm:"column:weight"`
	BodyMassIndex              float64   `gorm:"column:body_mass_index"`
	BodyMassIndexScore         float64   `gorm:"column:body_mass_index_score"`
	VitalCapacity              float64   `gorm:"column:vital_capacity"`
	VitalCapacityScore         float64   `gorm:"column:vital_capacity_score"`
	FiftyMeters                float64   `gorm:"column:50_meters"`
	FiftyMetersScore           float64   `gorm:"column:50_meters_score"`
	SitAndReach                float64   `gorm:"column:sit_and_reach"`
	SitAndReachScore           float64   `gorm:"column:sit_and_reach_score"`
	OneMinuteRopeSkipping      float64   `gorm:"column:1_minute_rope_skipping"`
	OneMinuteRopeSkippingScore float64   `gorm:"column:1_minute_rope_skipping_score"`
	OneMinuteSitUp             float64   `gorm:"column:1_minute_sit_up"`
	OneMinuteSipUpScore        float64   `gorm:"column:1_minute_sit_up_score"`
	CreatedAt                  time.Time `gorm:"column:created_at"`
}

func (*SelfEvaluation1) TableName() string {
	return "self_evaluation"
}
